﻿Imports System.Data.OleDb
Public Class frmLogin

#Region "NotifyIcon"

    Private Sub frmLogin_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Visible = False
                NotifyIcon.Visible = True
                NotifyIcon.ShowBalloonTip(1, "SkyCoffee", "SkyCoffee is Still Running!", ToolTipIcon.Info)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon.MouseDoubleClick
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuLaunchApplication_Click(sender As Object, e As EventArgs) Handles mnuLaunchApplication.Click
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuExitApplication_Click(sender As Object, e As EventArgs) Handles mnuExitApplication.Click
        Application.Exit()
    End Sub
#End Region

    Dim con As New OleDb.OleDbConnection
    Dim sql As String
    Dim dr As OleDb.OleDbDataReader
    Dim cmd As New OleDb.OleDbCommand
    Dim cmd1 As New OleDb.OleDbCommand
    Dim cmd2 As New OleDb.OleDbCommand
    Dim findID, newID As String

    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Loads frmAccountMenu
        pnlIcons.Controls.Clear()
        pnlIcons.Controls.Add(frmAccountMenu.pnlIcons)

        'Load ToolTips
        AccountToolTips()

        'Load tblAccountType into ComboBox
        Dim connString As String
        Dim connAccountType As New System.Data.OleDb.OleDbConnection()
        Dim daAccountType As New System.Data.OleDb.OleDbDataAdapter()
        Dim dsAccountType As DataSet

        connString = "Provider=Microsoft.jet.oledb.4.0;" & _
            "Data Source=|DataDirectory|\SkyCoffee.mdb"

        connAccountType = New System.Data.OleDb.OleDbConnection(connString)
        connAccountType.Open()
        daAccountType = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM tblAccountType", connAccountType)
        dsAccountType = New DataSet("Combo")
        daAccountType.Fill(dsAccountType, "Combo")
        cmbAccountType.DataSource = dsAccountType.Tables("Combo")

        cmbAccountType.LoadData() 'Load the ComboBox functions
    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtUsername.Text = "" Or txtPassword.Text = "" Then
            MsgBox("Please fill in all of the fields")
        Else
            findID = 0
            con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;" & _
                    "Data Source=|DataDirectory|\SkyCoffee.mdb")
            con.Open()

            sql = ""
            sql = "SELECT fldStaffNumber FROM tblAccounts WHERE fldAccountType='" & Trim(cmbAccountType.Value) & "'"
            cmd2.Connection = con
            cmd2.CommandText = sql
            dr = cmd2.ExecuteReader
            If (dr.HasRows) Then
                dr.Read()
                findID = dr.GetValue(0)
                dr.Close()
            End If

            sql = ""
            sql = "SELECT fldStaffNumber FROM tblAccounts WHERE fldStaffUsername='" & Trim(txtUsername.Text) & "'"
            cmd.Connection = con
            cmd.CommandText = sql
            dr = cmd.ExecuteReader
            If (dr.HasRows) Then
                dr.Read()
                findID = dr.GetValue(0)
                dr.Close()
            End If

            sql = ""
            sql = "SELECT fldStaffNumber FROM tblAccounts WHERE fldStaffPassword='" & txtPassword.Text & "' AND fldStaffNumber =" & findID & " "

            cmd1.Connection = con
            cmd1.CommandText = sql
            dr = cmd1.ExecuteReader
            If (dr.HasRows) Then
                dr.Read()
                newID = dr.GetValue(0)
                dr.Close()
            End If

            If (findID = newID) Then

                If cmbAccountType.Value = 1 Then

                    txtUsername.Text = ""
                    txtPassword.Text = ""

                    frmMain.Show()
                    Me.Close()
                    MsgBox("You have Successfully logged in as: Admin")

                ElseIf cmbAccountType.Value = 2 Then

                    txtUsername.Text = ""
                    txtPassword.Text = ""

                    frmMainManager.Show()
                    Me.Close()
                    MsgBox("You have Successfully logged in as: Manager")

                ElseIf cmbAccountType.Value = 3 Then

                    txtUsername.Text = ""
                    txtPassword.Text = ""

                    frmMainSupervisor.Show()
                    Me.Close()
                    MsgBox("You have Successfully logged in as: Supervisor")

                ElseIf cmbAccountType.Value = 4 Then

                    txtUsername.Text = ""
                    txtPassword.Text = ""

                    frmMainStaff.Show()
                    Me.Close()
                    MsgBox("You have Successfully logged in as: Staff")
                End If

            Else

                MsgBox("Login unsuccessful")
                txtUsername.Focus()

            End If
            End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        txtUsername.Text = ""
        txtPassword.Text = ""
    End Sub
End Class